const app = getApp();
var njzxw = require('../../../utils/njzxw.js');
var url = require('../../../utils/url.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    file: [],
    file_id: [],
    /**
     * 到港时间
     */
    date: '',
    fkcost: 0,
    cursor:0
  },
  inputText:function(e){
    this.setData({
      cursor:e.detail.cursor
    });
  },
  /**
     * 选择图片
     */
  chooseFile: function () {
    var $this = this;
    var cnt = $this.data.file.length;
    if (9 - cnt <= 0) {
      wx.showToast({
        title: '只能选择9张图片哦',
      })
      return;
    }

    wx.chooseImage({
      count: 9 - cnt < 0 ? 0 : 9 - cnt,
      sizeType: ['compressed'],
      success: function (res) {
        var cnt = res.tempFilePaths.length;

        $this.upload($this, 0, res.tempFilePaths);
      }
    });
  },
  upload: function ($this, i, list) {
    if (list.length <= i) {
      return;
    } else if (i == 0) {
      wx.showLoading();
    }
    var path = list[i];
    wx.uploadFile({
      url: url.singleUploadFile,
      filePath: path,
      name: 'file_',
      success: function (res) {
        var dat = [];
        var dat_id = [];
        dat[0] = path;
        console.log(res);
        dat_id[0] = JSON.parse(res.data).data.id;
        $this.setData({
          file: $this.data.file.concat(dat),
          file_id: $this.data.file_id.concat(dat_id),
        });
        if (list.length > i + 1) {
          $this.upload($this, i + 1, list);
        } else {
          wx.hideLoading();
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '上传失败',
        })
        wx.hideLoading();
      }
    });
  },

  deleteFile: function (e) {
    var file = this.data.file.splice(e.currentTarget.dataset.num, 1);
    var file_id = this.data.file_id.splice(e.currentTarget.dataset.num, 1);
    var $this = this;
    this.setData({
      file: $this.data.file
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var $this = this;
    njzxw.addSubmit({
      page:this,
      checkUser:true,
      url: url.userConsultationInfoFront.save,
      verify: function (b) {
        console.log(b);
        if (b.name == '') {
          return "请输入您的姓名";
        }
        if (b.phone == '') {
          return "请输入您的联系电话";
        }
        if (b.context == '') {
          return "在咨询内容里面填写点东西吧";
        }
        b.name = njzxw.emoji(b.name);
        b.context = njzxw.emoji(b.context);
        b.expert_id = $this.data.id;
        b.resouce_ids = $this.data.file_id;
        return b;
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})